﻿using System;
using System.Diagnostics.Contracts;
using System.Windows;
using System.Windows.Navigation;
using DaveSexton.Labs;
using Microsoft.Phone.Controls;

namespace Rxx.Labs
{
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable",
		Justification = "The Closing event is used to dispose of resources")]
	public partial class App : Application
	{
		#region Public Properties
		#endregion

		#region Private / Protected
		internal PhoneLabController LabController
		{
			get
			{
				Contract.Ensures(Contract.Result<PhoneLabController>() != null);

				Contract.Assume(controller != null);

				return controller;
			}
		}

		private PhoneLabController controller;
		private PhoneApplicationFrame rootFrame;
		private bool phoneApplicationInitialized;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="App" /> class.
		/// </summary>
		public App()
		{
			UnhandledException += Application_UnhandledException;

			// Show graphics profiling information while debugging.
			if (System.Diagnostics.Debugger.IsAttached)
			{
				// Display the current frame rate counters
				Application.Current.Host.Settings.EnableFrameRateCounter = true;

				/* Show the areas of the app that are being redrawn in each frame.
				 * Application.Current.Host.Settings.EnableRedrawRegions = true;
				 */

				/*Enable non-production analysis visualization mode, 
				 * which shows areas of a page that are being GPU accelerated with a colored overlay.
				 * Application.Current.Host.Settings.EnableCacheVisualization = true;
				 */
			}

			InitializeComponent();

			InitializePhoneApplication();
		}
		#endregion

		#region Methods
		private void InitializePhoneApplication()
		{
			if (phoneApplicationInitialized)
				return;

			rootFrame = new PhoneApplicationFrame();

			rootFrame.Navigated += CompleteInitializePhoneApplication;
			rootFrame.NavigationFailed += RootFrame_NavigationFailed;

			phoneApplicationInitialized = true;
		}
		#endregion

		#region Event Handlers
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode",
			Justification = "Referenced in XAML.")]
		private void Application_Launching(object sender, Microsoft.Phone.Shell.LaunchingEventArgs e)
		{
			controller = new PhoneLabController(new RxxLabCatalog());
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode",
			Justification = "Referenced in XAML.")]
		private void Application_Closing(object sender, Microsoft.Phone.Shell.ClosingEventArgs e)
		{
			controller.Dispose();
		}

		private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
		{
			if (System.Diagnostics.Debugger.IsAttached)
			{
				System.Diagnostics.Debugger.Break();
			}
		}

		private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
		{
			if (System.Diagnostics.Debugger.IsAttached)
			{
				System.Diagnostics.Debugger.Break();
			}
		}

		private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
		{
			if (RootVisual != rootFrame)
				RootVisual = rootFrame;

			rootFrame.Navigated -= CompleteInitializePhoneApplication;
		}
		#endregion
	}
}